package com.shk.service.imp;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shk.dao.PermissionDao;
import com.shk.dao.RoleDao;
import com.shk.dao.UserDao;
import com.shk.entity.PageResult;
import com.shk.pojo.Permission;
import com.shk.pojo.Role;
import com.shk.pojo.User;
import com.shk.service.UserService;
import com.shk.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        /*通过姓名拿到用户信息*/
       User user= userDao.findByUserName(username);
       if(user==null){
           return null;
       }
        Integer userId = user.getId();
        /*通过user  id 拿到角色信息*/
       Set<Role> roles= roleDao.findByUserId(userId);
       if(roles!=null && roles.size()>0){
           for(Role role:roles){
               Integer roleId = role.getId();
               Set<Permission> permissions = permissionDao.findByRoleId(roleId);
               role.setPermissions(permissions);
           }
           user.setRoles(roles);
       }

        return user;

    }

    @Override
    public List<Map<String, Object>> findMean(String username) {
        //根据用户的用户名查询该用户的权限信息
        List<Map<String, Object>> mean = userDao.findMean(username);
        //整理数据
        List<Map<String,Object>> listFu = new ArrayList<>();
        List<Map<String,Object>> listZi = new ArrayList<>();

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("path", "1");
        stringObjectHashMap.put("title","工作台");
        stringObjectHashMap.put("icon","fa-dashboard");
        stringObjectHashMap.put("children", new ArrayList<>());

        listFu.add(stringObjectHashMap);


        //封装子目录
        for (Map<String, Object> map : mean) {
            Integer parentMenuId = (Integer) map.get("parentMenuId");
            if (parentMenuId != null ){
                //子目录
                Map<String, Object> mapZi = new HashMap<>();
                //不等于null 就说明是子目录
                mapZi.put("parentMenuId",(Integer)map.get("parentMenuId"));
                mapZi.put("path",map.get("path"));
                mapZi.put("title",map.get("title"));
                mapZi.put("linkUrl",map.get("linkUrl"));
                mapZi.put("children",new ArrayList<Map>());
                listZi.add(mapZi);
            }else{
                //父目录
                Map<String, Object> mapFu = new HashMap<>();
                //说明是父目录
                mapFu.put("id",(Integer)map.get("id"));
                mapFu.put("path",map.get("path"));
                mapFu.put("title",map.get("title"));
                mapFu.put("children",new ArrayList<Map>());
                mapFu.put("icon",map.get("icon"));
                listFu.add(mapFu);
            }
        }

        //封装父目录的 children 属性
        for (Map<String, Object> mapf : listFu) {
            Integer id = (Integer) mapf.get("id");
            for (Map<String, Object> mapz : listZi) {
                Integer parentMenuId = (Integer) mapz.get("parentMenuId");
                if (id == parentMenuId){
                    List<Map> children = (List<Map>) mapf.get("children");
                    children.add(mapz);
                }
            }
        }


        return listFu;
    }


    /**
     * 查出生日
     * @return
     */
    @Override
    public List<Map<String, Object>> findByAge() {
        List<Map<String,Object>> map = userDao.finByAge();
        return map;
    }



    @Override
    public void add(User user, Integer[] roleIds) {
        String password = MD5Utils.md5(user.getPassword());
        String deleteCode = user.getDeleteCode();
        String gender = user.getGender();
        System.out.println(gender);
        if (deleteCode == null) {
            user.setDeleteCode("0");
        }
        user.setPassword(password);
        userDao.add(user);
        setUserAndRole(user.getId(),roleIds);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<User> data=userDao.findPage(queryString);

        return new PageResult(data.getTotal(),data.getResult());
    }

    @Override
    public void delete(Integer id) {
        deleteUserAndRole(id);
        userDao.delete(id);
    }

    @Override
    public void update(User user, Integer[] roleIds) {
        deleteUserAndRole(user.getId());
        userDao.update(user);
        setUserAndRole(user.getId(),roleIds);
    }

    @Override
    public Map findRoleIdsByUserId(Integer userId) {
        Map map = new HashMap<>();
        List<Role> all = roleDao.findAll();
        User user = userDao.findById(userId);
        List<Integer> list= userDao.findRoleByUserId(userId);
        map.put("roles",all);
        map.put("user",user);
        map.put("roleIds",list);
        return map;
    }


    /*删除用户和角色关系*/
    public void deleteUserAndRole(Integer id){
        userDao.deleteUserAndRole(id);
    }
    /*建立用户和角色关系*/
    public void setUserAndRole(Integer userId,Integer[] roleIds){
        if(userId !=null){
            if(roleIds != null && roleIds.length>0){
                for(Integer roleId:roleIds){
                    Map map = new HashMap<>();
                    map.put("user_id",userId);
                    map.put("role_id",roleId);
                    userDao.setUserAndRole(map);
                }
            }
        }
    }
}
